<template>
  <div class="mv-most-expected">
    <h1>近期最受欢迎</h1>
    <!-- 滚动列表开始 -->
    <div id="scroll">
      <ul>
        <li v-for="(li, index) in expectedList" :key="index">
          <img :src="li.img" alt="" />
          <h2>{{ li.nm }}</h2>
          <h3>{{ li.wish }}人想看</h3>
          <h3>{{ li.comingTitle }}</h3>
        </li>
      </ul>
    </div>
    <!-- 滚动列表结束 -->
  </div>
</template>

<script>
import axios from "@/api/instance";
import BetterScroll from "better-scroll";
export default {
  data() {
    return {
      expectedList: [],
    };
  },
  methods: {
    // 用来加载列表的数据
    async loadData() {
      this.loaded = false;
      let result = await axios.get("/mostExpected.json");
      // console.log('近期最受欢迎数据：', result);
      // 追加
      this.hasMore = !!result?.paging?.hasMore;
      this.expectedList = [...this.expectedList, ...result.coming];
      this.loaded = true;
    },
  },
  created() {
    // this => MostExpected 组件 => 对象
    //
    this.offset = 0;
    this.limit = 10;
    this.loaded = false;
  },
  async mounted() {
    await this.loadData();
    // 等待子元素結構生成
    await this.$nextTick(); // 待會介紹這個作用
    // 使用 better-scroll 初始化元素
    let bscroll = new BetterScroll("#scroll", {
      scrollX: true,
    });
    console.log(this); // MostExptected
    // 注册滚动停止的时候，加载更多的数据
    // bscroll.on('scrollEnd', () => {
    // 	console.log('滚动结束了');
    // 	// 加载数据
    // 	console.log(this);
    // });
    // 注意，回调函数这一部分我们不是使用箭头函数去写
    let that = this;
    bscroll.on("scrollEnd", async function ({ x }) {
      console.log("x 轴滚动的距离:", x);
      console.log("scrollEnd:", this);
      console.log(this.maxScrollX);
      // 加载数据
      // await that.loadData();
      // 重新让新的DOM 可以滚动
      // this.refresh();
      // -1 -2
      // 1< 2
      if (x <= this.maxScrollX && that.loaded && that.hasMore) {
        console.log("滚动到右边触底");
        await that.loadData();
        // 重新让新的DOM 可以滚动
        this.refresh();
      }
    });
  },
};
</script>

<style lang="less" scoped>
@import url("@/assets/ellipsis.less");
.mv-most-expected {
  margin: 0 0.15rem;
  h1 {
    padding: 0.15rem 0.15rem 0.15rem 0;
    font-size: 0.18rem;
    font-weight: normal;
  }
  #scroll {
    ul {
      display: flex;
      width: max-content;
      li {
        width: 0.85rem;
        margin-right: 0.15rem;
        img {
          width: 0.85rem;
          height: 1.1rem;
        }
        h2 {
          .ellipsis(100%);
          font-size: 0.16rem;
          line-height: 0.3rem;
        }

        h3 {
          .ellipsis(100%);
          color: #666;
        }
      }
    }
  }
}
</style>